function VisualizeDigits(Y1,Y2,X,thresh,imsize)

if (nargin<4)
  thresh = 0.55;
  imsize = 0.04;
end;

min1 = min(Y1);
min2 = min(Y2);
max1 = max(Y1);
max2 = max(Y2);

figure(1); clf;
axis([min1 max1 min2 max2]);
hold on;
include = 1:size(X,2);%[1];
plot(Y1(1),Y2(1),'o','EraseMode','none'); 
drawnow;

[tmp,ii] = sort(Y1); 
for jj=ii(1:end)
  d1 = abs(Y1(jj)-Y1(include));
  d2 = abs(Y2(jj)-Y2(include));
  dist = min(max([d1;d2]));
  if (dist>thresh)
    include = [include jj];
    plot(Y1(jj),Y2(jj),'o','EraseMode','none'); 
    drawnow;
  end;
end;

figure(1); clf; axis square off;
set(gcf,'PaperPositionMode','auto','Resize','off');
offset = 0.05;
set(gca,'Position',[offset offset 1-2*offset 1-2*offset]);
Z1 = (1-2*offset)*(Y1-min1)/(max1-min1) + offset;
Z2 = (1-2*offset)*(Y2-min2)/(max2-min2) + offset;
%plot(Z1,Z2,'.');
%hold on;
%axis tight;

side = sqrt(size(X,1));
for jj=include
  z1 = Z1(jj);
  z2 = Z2(jj);
  aa = axes('Position',[z1-imsize/2 z2-imsize/2 imsize imsize]); 
  axes(aa);
  imagesc(reshape(X(:,jj),[side side]));
  colormap hot;
  axis off;
%  drawnow;
end;
return;
